﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Enums;
using mozhi.smarterp.Finance.Treasuries.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Treasuries.Payments
{
    public class PaymentRequisitionRepository : EfCoreRepository<FinanceDbContext, PaymentRequisition, Guid>, IPaymentRequisitionRepository
    {
        public PaymentRequisitionRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<long> GetCountAsync(string keyword, PaymentRequisitionType? type, bool? isAdvance, DateTime? startDate, DateTime? endDate, Guid? currencyId, Guid? excludeId,
            Guid? sourceDocumentId, Guid? sourceDocumentEntryId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = sourceDocumentId.HasValue || sourceDocumentEntryId.HasValue ? dbContext.Set<PaymentRequisition>().Include(x => x.Items).AsNoTracking() : dbContext.Set<PaymentRequisition>().AsNoTracking();

            var count = await query
                .WhereIf(!string.IsNullOrEmpty(keyword), x => x.Number.Contains(keyword))
                .WhereIf(type.HasValue, x => x.Type == type)
                .WhereIf(isAdvance.HasValue, x => x.IsAdvanced == isAdvance)
                .WhereIf(startDate.HasValue, x => x.Date >= startDate)
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .WhereIf(excludeId.HasValue, x => x.Id != excludeId)
                .WhereIf(sourceDocumentId.HasValue, x => x.Items.Any(y=>y.SourceDocumentId == sourceDocumentId))
                .WhereIf(sourceDocumentEntryId.HasValue, x => x.Items.Any(y => y.SourceDocumentEntryId == sourceDocumentEntryId))
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<string> GetLatestNumberAsync()
        {
            var dbContext = await GetDbContextAsync();

            var latestNumber = dbContext.Set<PaymentRequisition>().AsNoTracking().Max(x => x.Number);

            return latestNumber;
        }

        public async Task<List<PaymentRequisition>> GetListAsync(string keyword, PaymentRequisitionType? type, bool? isAdvance, DateTime? startDate, DateTime? endDate, Guid? currencyId,
            Guid? sourceDocumentId, Guid? sourceDocumentEntryId, bool incldueDetials = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = sourceDocumentId.HasValue || sourceDocumentEntryId.HasValue || incldueDetials ? dbContext.Set<PaymentRequisition>().Include(x => x.Items).AsNoTracking() : dbContext.Set<PaymentRequisition>().AsNoTracking();

            var entities = await query
                .WhereIf(!string.IsNullOrEmpty(keyword), x => x.Number.Contains(keyword))
                .WhereIf(type.HasValue, x => x.Type == type)
                .WhereIf(isAdvance.HasValue, x => x.IsAdvanced == isAdvance)
                .WhereIf(startDate.HasValue, x => x.Date >= startDate)
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .WhereIf(sourceDocumentId.HasValue, x => x.Items.Any(y => y.SourceDocumentId == sourceDocumentId))
                .WhereIf(sourceDocumentEntryId.HasValue, x => x.Items.Any(y => y.SourceDocumentEntryId == sourceDocumentEntryId))
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .ToListAsync(cancellationToken);

            return entities;
        }

        public async Task<List<PaymentRequisition>> GetPagedListAsync(string keyword, PaymentRequisitionType? type, bool? isAdvance, DateTime? startDate, DateTime? endDate, Guid? currencyId, Guid? sourceDocumentId, Guid? sourceDocumentEntryId,
            int maxResultCount, int sikpCount, string sorting, bool incldueDetials = false, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var query = sourceDocumentId.HasValue || sourceDocumentEntryId.HasValue || incldueDetials ? dbContext.Set<PaymentRequisition>().Include(x => x.Items).AsNoTracking() : dbContext.Set<PaymentRequisition>().AsNoTracking();

            var entities = await query
                .WhereIf(!string.IsNullOrEmpty(keyword), x => x.Number.Contains(keyword))
                .WhereIf(type.HasValue, x => x.Type == type)
                .WhereIf(isAdvance.HasValue, x => x.IsAdvanced == isAdvance)
                .WhereIf(startDate.HasValue, x => x.Date >= startDate)
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .WhereIf(currencyId.HasValue, x => x.CurrencyId == currencyId)
                .WhereIf(sourceDocumentId.HasValue, x => x.Items.Any(y => y.SourceDocumentId == sourceDocumentId))
                .WhereIf(sourceDocumentEntryId.HasValue, x => x.Items.Any(y => y.SourceDocumentEntryId == sourceDocumentEntryId))
                .WhereIf(endDate.HasValue, x => x.Date <= endDate)
                .OrderByIf<PaymentRequisition, IQueryable<PaymentRequisition>>(!sorting.IsNullOrEmpty(), sorting)
                .PageBy(sikpCount, maxResultCount)
                .ToListAsync(cancellationToken);

            return entities;
        }
    }
}
